//
//  SoundViewController.m
//  HuiYinMaster
//
//  Created by Icloud-Zhanyong on 2017/4/16.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "SoundViewController.h"
#import "SoundListTableViewCell.h"
#import "SoundDetailViewController.h"
#import "SeverManager.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"
#import "SearchSoundViewController.h"
#import "VoiceListModel.h"
#import "AFNetWorking.h"
#import "SoundPlayTool.h"

@interface SoundViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    int i;
    int size;
    //每次刷新给playID加size个基数
    int total;
}
@end

@implementation SoundViewController

-(NSMutableArray<VoiceListModel *> *)voiceListDataArray {
    if (_voiceListDataArray == nil) {
        _voiceListDataArray = [[NSMutableArray alloc]init];
    }
    return _voiceListDataArray;
}

- (void)viewDidLoad {
    
    i = 0;
    size = 5;
    [super viewDidLoad];
    [self CreatUI];
    [self loadData];
    [self addFooterRefresh];
}
-(void)XMRefreshLoadMore {
    i = (int)self.voiceListDataArray.count;
    [self loadData];
}

-(void)CreatUI{
    self.BaseTableView.delegate = self;
    self.BaseTableView.dataSource = self;
    self.BaseTableView.showsVerticalScrollIndicator = false;
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"全部声音";
    UIButton * serarchBtn = [[UIButton alloc]init];
    [serarchBtn setImage:[UIImage imageNamed:@"icon_search"] forState:UIControlStateNormal];
    [serarchBtn sizeToFit];
    [serarchBtn addTarget: self action:@selector(serachBtnClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:serarchBtn];
    [self addCenterButton];
}
-(void)loadData {
    NSDictionary * param = @{
                             @"i" : @(i),
                             @"size" : @(size)
                             };
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setValue:appId forHTTPHeaderField:@"appId"];
    [manager POST:voiceList parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"全部声音%@",responseObject);
        NSArray * array  = responseObject[@"data"];
        if (array.count > 0) {
            for (int j = 0;j < array.count;j++) {
                NSDictionary * dic = array[j];
                VoiceListModel * model = [[VoiceListModel alloc]init];
                [model setValuesForKeysWithDictionary:dic];
                model.headerImageUrl = self.headerImgUrl;
                model.playID = j + total;
                NSLog(@"%d",model.playID);
                [self.voiceListDataArray addObject:model];
            }
            total += size;
            
            [self.BaseTableView reloadData];
        } else {
            self.noMoredata = YES;
        }
        self.isRefresh = NO;
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        self.isRefresh = NO;
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.voiceListDataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SoundListTableViewCell * cell = [SoundListTableViewCell CellWith:tableView];
    
    [cell.SoundListImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",port,self.voiceListDataArray[indexPath.row].imgUrl]]];
    cell.SoundTitleLabel.text = self.voiceListDataArray[indexPath.row].albumTitle;
    cell.SoundCreatTime.text = [self.voiceListDataArray[indexPath.row].createTime substringToIndex:10];
    cell.SoundPlaytimes.text = [NSString stringWithFormat:@"%d 万",self.voiceListDataArray[indexPath.row].playTimes];
    cell.SoundCurrentTime.text = [NSString stringWithFormat:@"%2d:%2d",self.voiceListDataArray[indexPath.row].duration / 60,self.voiceListDataArray[indexPath.row].duration % 60];
    cell.SoundClassesLabel.text = self.voiceListDataArray[indexPath.row].title;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_HEIGHT / 4;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SoundDetailViewController * vc = [[SoundDetailViewController alloc]init];
    vc.voiceDetailModel = self.voiceListDataArray[indexPath.row];
    vc.voiceDataArray = self.voiceListDataArray;
    [self.navigationController pushViewController:vc animated:YES];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)serachBtnClick{
    SearchSoundViewController * searchAlbumVC = [[SearchSoundViewController alloc]init];
    if (self.voiceListDataArray.count > 0){
    searchAlbumVC.headImgUrl = self.voiceListDataArray[0].headerImageUrl;
    }
    [self.navigationController pushViewController:searchAlbumVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
